// Copyright 2007 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lichen.components.base;

import lichen.pages.base.LogInfoPage;
import lichen.services.LichenModule;
import lichen.services.user.UserService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.TapestryConstants;
import org.apache.tapestry.annotations.BeforeRenderTemplate;
import org.apache.tapestry.annotations.Inject;
import org.apache.tapestry.annotations.Parameter;
import org.apache.tapestry.ioc.services.SymbolSource;
import org.apache.tapestry.services.Request;

/**
 * 论坛的整体的布局
 * 
 * @author <a href="mailto:jun.tsai@gmail.com">Jun Tsai</a>
 * @version $Revision: 7 $
 * @since 0.0.1
 */
public class AbstractLayout {
	/**
	 * Logger for this class
	 */
	private static final Log logger = LogFactory.getLog(AbstractLayout.class);

	/**
	 * 指定对应的url前缀，默认为contextpath前缀
	 */
	@Parameter(defaultPrefix = TapestryConstants.LITERAL_BINDING_PREFIX)
	private String siteUrlPrefix;

	private static final String DEFAULT_TEMPLATE_PATH = "/skin/default";

	@Inject
	private Request request;

	@Inject
	private UserService userService;

	@Inject
	private ComponentResources componentResources;

	/** 表头信息 * */
	private String _logInfo;

	/**
	 * 设定当前页面的title信息
	 */
	@Parameter(defaultPrefix = TapestryConstants.LITERAL_BINDING_PREFIX)
	private String _title;

	
	@Inject
	private SymbolSource symbolSource;
	
	


	/**
	 * 得到模板的url.
	 * 
	 * @return 模板的url.
	 */
	public String getTemplateUrl() {
		if (siteUrlPrefix == null) {
			siteUrlPrefix = request.getContextPath();
		}
		return siteUrlPrefix + DEFAULT_TEMPLATE_PATH;
	}

	/**
	 * 得到展示的标题文字.
	 * @return 页面的标题文字.
	 */
	public String getTitle() {

		if (_title == null && _logInfo != null) {
			_title = _logInfo;
		}
		return this._title;
	}

	public long getOnlineUserNum() {
		return userService.getOnlineUserNum();
	}

	/**
	 * 
	 * 激活用户的当前状态
	 *
	 */
	@BeforeRenderTemplate
	void activeOnlineUsers() {
		if (logger.isDebugEnabled()) {
			logger.debug("active online user info"); //$NON-NLS-1$
		}

		org.apache.tapestry.runtime.Component obj = componentResources.getPage();
		if (LogInfoPage.class.isInstance(obj)) {
			_logInfo = ((LogInfoPage) obj).getLogInfo();
			if (logger.isDebugEnabled()) {
				logger.debug(" log page info [ " + _logInfo + "]"); //$NON-NLS-1$
			}
			userService.activeOnlineUser(_logInfo);
		}
	}
	public String getVersion(){
		//TODO tapestyr 使用 @Inject @Value 目前还有问题，所以只好采用折中的方式
		return symbolSource.expandSymbols("${"+LichenModule.LICHEN_VERSION_KEY_NAME+"}");
	}


}
